package com.jinghua.treatment.service.impl;

import com.jinghua.treatment.entity.SysInquiryAnswersSub;
import com.jinghua.treatment.entity.SysInquiryAnswersSubExample;
import com.jinghua.treatment.mapper.SysInquiryAnswersSubMapper;
import com.jinghua.treatment.service.SysInquiryAnswersSubService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 王兴光 on 2018/5/25.
 */
@Service
public class SysInquiryAnswersSubServiceImpl implements SysInquiryAnswersSubService {
    @Resource
    private SysInquiryAnswersSubMapper inquiryAnswersSubMapper;

    @Override
    public int insertSelective(String radioValue,String checkboxValue,String textValue,Integer inquiryId) {
       // 定义存储二级分类编码与三级分类编码的列表
        Map<String,String> answersMap = new HashMap<String,String>();
        // 定义临时存储键值对的变量
        String key ="",value = "";
        System.out.println("截取钱："+textValue);
        // 截取字符串，去掉最后一个逗号
        if(textValue.endsWith(",")){
            textValue = textValue.substring(0,textValue.length()-1);
        }
        System.out.println("截取后："+textValue);
        String[] textValueList = textValue.split(",");    // 根据分隔符号拆分字符串
        if(!textValue.isEmpty()) {
            for (String tValue : textValueList) {
                System.out.println(tValue);
                // 命名规则固定，二级分类编号-文本内容
                answersMap.put(tValue.substring(0, tValue.indexOf("-")),tValue.substring(tValue.indexOf("-")+1));
            }
        }
        // 截取字符串，去掉最后一个逗号
        if(radioValue.endsWith(",")){
            radioValue = radioValue.substring(0,radioValue.length()-1);
        }
        String[] radioValueList = radioValue.split(",");    // 根据分隔符号拆分字符串
        if(!radioValue.isEmpty()) {
            for (String rValue : radioValueList) {
                // 命名规则固定，三级分类编码的前四位是二级分类编码
                answersMap.put(rValue.substring(0, 4),rValue);
            }
        }
        if(checkboxValue.endsWith(",")){
            checkboxValue = checkboxValue.substring(0,checkboxValue.length()-1);
        }
        String[] checkboxValueList = checkboxValue.split(",");
        if(!checkboxValue.isEmpty()) {
            for (String cValue : checkboxValueList) {
                // 验证是否已有相同的二级分类编码
                if (cValue.substring(0, 4).equals(key)) {
                    value += "," + cValue;
                } else {
                    if (!"".equals(value)) {
                        answersMap.put(key, value);
                    }
                    value = cValue;
                }
                key = cValue.substring(0, 4);
            }
        }
        // 定义answersSub对象
        SysInquiryAnswersSub sub = new SysInquiryAnswersSub();
        // 迭代map集合
        for (String level2Code : answersMap.keySet()){
            // 根据键获取值
            sub.setSubjectCode(level2Code);
            sub.setAnswers(answersMap.get(level2Code));
            sub.setRecordId(inquiryId);
            inquiryAnswersSubMapper.insertSelective(sub);   // 调用添加方法
        }
        return 1;
    }

    @Override
    public List<SysInquiryAnswersSub> selectByExample(SysInquiryAnswersSubExample example) {
        return inquiryAnswersSubMapper.selectByExample(example);
    }

    @Override
    public List<SysInquiryAnswersSub> selectByDoctorId(Integer doctorId) {
        return inquiryAnswersSubMapper.selectByDoctorId(doctorId);
    }

    @Override
    public SysInquiryAnswersSub selectByPrimaryKey(Integer uuid) {
        return null;
    }

    @Override
    public int updateByExampleSelective(@Param("record") SysInquiryAnswersSub record, @Param("example") SysInquiryAnswersSubExample example) {
        return 0;
    }

    @Override
    public int updateByExample(@Param("record") SysInquiryAnswersSub record, @Param("example") SysInquiryAnswersSubExample example) {
        return 0;
    }

    @Override
    public int updateByPrimaryKeySelective(SysInquiryAnswersSub record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(SysInquiryAnswersSub record) {
        return 0;
    }
}
